﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Bet_Yatziv
{
    public partial class Costumers : Form
    {
        //init
        public Costumers()
        {
            InitializeComponent();
            refresh();
        }

        //exit
        private void btn_return(object sender, EventArgs e)
        {
            this.Close();
        }

        //load
        private void btn_load(object sender, EventArgs e)
        {
            DataTable table = DataAccessLayer.GetAllCostumers();
            dataGridView1.DataSource = table;
        }

        //clear
        private void btn_clear(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
            clearAll();
            disableText();
            maindelete.Enabled = true;
            mainupdate.Enabled = true;
            Binsert.Enabled = true;
            unshowOK();
        }

        //add
        public void Add()
        {
            if (txt_ID.Text == "")
                MessageBox.Show("אנא הכנס תעודת זהות לקוח", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                if (txt_fName.Text == "")
                    MessageBox.Show("אנא הכנס שם פרטי", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    if (txt_lName.Text == "")
                        MessageBox.Show("אנא הכנס שם משפחה", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        if (txt_address.Text == "")
                            MessageBox.Show("אנא הכנס כתובת", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                            if (txt_payment.Text == "")
                                MessageBox.Show("אנא הכנס אמצעי תשלום", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
            {
                string id = txt_ID.Text;
                string fname = txt_fName.Text;
                string lname = txt_lName.Text;
                string adress = txt_address.Text;
                string hphone = txt_homePhone.Text;
                string cphone = txt_cellPhone.Text;
                string pmethod = txt_payment.Text;

                int idNum = 0;
                if (id.Length > 0)
                {
                    //converting id number
                    try
                    {
                        idNum = Convert.ToInt32(id);
                    }
                    catch (FormatException exp)
                    {
                        MessageBox.Show(exp.Message + "אנא הכנס תעודת זהות לקוח בתור מספר", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txt_ID.Text = "";
                        return;
                    }
                }

                if (costumerSearch(idNum))
                {
                    MessageBox.Show("הלקוח כבר קיים במערכת", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {

                    DataAccessLayer.AddNewCost(idNum, fname, lname, adress, hphone, cphone, pmethod);
                    MessageBox.Show("כרטיס לקוח נוצר בהצלחה", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Binsert.Enabled = true;
                    disableText();
                    refresh();
                }
            }
        }

        //update
        public void Updatee()
        {
            if (txt_ID.Text == "")
                MessageBox.Show("אנא הכנס תעודת זהות לקוח", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                if (txt_fName.Text == "")
                    MessageBox.Show("אנא הכנס שם פרטי", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    if (txt_lName.Text == "")
                        MessageBox.Show("אנא הכנס שם משפחה", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        if (txt_address.Text == "")
                            MessageBox.Show("אנא הכנס כתובת", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        else
                            if (txt_payment.Text == "")
                                MessageBox.Show("אנא הכנס אמצעי תשלום", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            else
                            {
                                string id = txt_ID.Text;
                                string fname = txt_fName.Text;
                                string lname = txt_lName.Text;
                                string adress = txt_address.Text;
                                string hphone = txt_homePhone.Text;
                                string cphone = txt_cellPhone.Text;
                                string pmethod = txt_payment.Text;

                                int idNum = 0;
                                //converting id number
                                if (id.Length > 0)
                                {
                                    try
                                    {
                                        idNum = Convert.ToInt32(id);
                                    }
                                    catch (FormatException exp)
                                    {
                                        MessageBox.Show(exp.Message + "אין אפשרות להמיר מספר", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        txt_ID.Text = "";
                                        return;
                                    }
                                }

                                if (costumerSearch(idNum))
                                {
                                    DataAccessLayer.UpdateCost(idNum, fname, lname, adress, hphone, cphone, pmethod);
                                    MessageBox.Show("פרטי לקוח עודכנו בבהצלחה", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    refresh();
                                    mainupdate.Enabled = true;
                                }
                                else
                                {
                                    MessageBox.Show("הלקוח לא קיים במערכת", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                }
                            }
        }

        //delete
        public void Deletee()
        {
            string id = txt_ID.Text;
            int idNum = 0;

            //converting id number
            if (id.Length > 0)
            {
                try
                {
                    idNum = Convert.ToInt32(id);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "ערך תעודת הזהות חייב להיות מספר", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_ID.Text = "";
                    return;
                }
            }

            if (costumerSearch(idNum))
            {
                if (MessageBox.Show("מחיקת הלקוח תמחק את כל המידע הרלוונטי אליו במערכת, האם אתה בטוח שברצונך למחוק את הלקוח", "מחיקת לקוח", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DataAccessLayer.DeleteCost(idNum);
                    MessageBox.Show("לקוח נמחק בהצלחה", "הודעה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    maindelete.Enabled = true;
                    refresh();
                }
            }
            else
            {
                MessageBox.Show("לקוח לא קיים", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //refreshing all the data
        private void refresh()
        {
            dataGridView1.DataSource = null;
            DataTable dt = DataAccessLayer.GetAllCostumers();
            dataGridView1.DataSource = dt;
            clearAll();

            unshowOK();

            txt_ID.Items.Clear();
            txt_payment.Items.Clear();

            string query1 = "select costumerID from tbl_costumers";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            for (int i = 0; i < dt1.Rows.Count; i++)
                txt_ID.Items.Add(dt1.Rows[i].ItemArray[0]);

            txt_payment.Items.Add("ויזה");
            txt_payment.Items.Add("דירקט");
            txt_payment.Items.Add("מסטרקארד");
            txt_payment.Items.Add("אמריקן אקספרס");
            txt_payment.Items.Add("ישראקארד");
        }

        //main add button
        private void btn_add(object sender, EventArgs e)
        {
            maindelete.Enabled = true;
            enableText();
            Binsert.Enabled = false;
            mainupdate.Enabled = true;
            clearAll();
            showOK();
        }

        //main delete button
        private void btn_delete(object sender, EventArgs e)
        {
            maindelete.Enabled = false;
            Binsert.Enabled = true;
            disableText();
            mainupdate.Enabled = true;
            showOK();
        }

       //main update button
        private void btn_update(object sender, EventArgs e)
        {
            maindelete.Enabled = true;
            Binsert.Enabled = true;
            enableText();
            mainupdate.Enabled = false;
            showOK();
        }

        //bringing up all costumer details when choosing his id on the combo box
        private void txt_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = 0;
            string query1 = "select * from tbl_costumers";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);

            int ideNum = 0;
            if (txt_ID.Text.Length > 0)
            {
                try
                {
                    ideNum = Convert.ToInt32(txt_ID.Text);
                }
                catch (FormatException exp)
                {
                    MessageBox.Show(exp.Message + "אין אפשרות להמיר את ערך תעודת הזהות", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txt_ID.Text = "";
                    return;
                }
            }

            for (i = 0; i < dt1.Rows.Count; i++)
                if (ideNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            txt_fName.Text = "" + dt1.Rows[i].ItemArray[1];
            txt_lName.Text = "" + dt1.Rows[i].ItemArray[2];
            txt_address.Text = "" + dt1.Rows[i].ItemArray[3];
            txt_homePhone.Text = "" + dt1.Rows[i].ItemArray[4];
            txt_cellPhone.Text = "" + dt1.Rows[i].ItemArray[5];
            txt_payment.Text = "" + dt1.Rows[i].ItemArray[6];

        }

        //clearing all textboxes
        public void clearAll()
        {
            txt_lName.Text = txt_fName.Text = txt_address.Text = txt_ID.Text = "";
            txt_searchID.Text = txt_payment.Text = txt_cellPhone.Text = txt_homePhone.Text = "";
        }

        //searching costumer by ID number
        private void btn_advanceSearch(object sender, EventArgs e)
        {
            
            if (txt_searchID.Text == "")
                MessageBox.Show("לא הוכנס תז לקוח", "שים לב", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
            {
                try
                {
                    string query = "SELECT tbl_costumers.costumerID, tbl_costumers.firstName, tbl_costumers.lastName, tbl_costumers.address, tbl_costumers.homePhone, tbl_costumers.cellPhone, tbl_costumers.payingMethod FROM tbl_costumers WHERE (((tbl_costumers.costumerID)=" + txt_searchID.Text + "))" + "";
                    DataTable table = AdoHelper.ExecuteDataTable(query);
                    dataGridView1.DataSource = table;

                    txt_ID.Text = "" + table.Rows[0].ItemArray[0];
                    txt_fName.Text = "" + table.Rows[0].ItemArray[1];
                    txt_lName.Text = "" + table.Rows[0].ItemArray[2];
                    txt_address.Text = "" + table.Rows[0].ItemArray[3];
                    txt_homePhone.Text = "" + table.Rows[0].ItemArray[4];
                    txt_cellPhone.Text = "" + table.Rows[0].ItemArray[5];
                    txt_payment.Text = "" + table.Rows[0].ItemArray[6];
                }
                catch
                {
                    MessageBox.Show("לקוח לא קיים", "שגיאה", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        //main ok button
        private void btn_ok(object sender, EventArgs e)
        {
            if (maindelete.Enabled == false)   
                Deletee();
            if (mainupdate.Enabled == false)  
                Updatee();
            if (Binsert.Enabled == false)
                Add();
            }

        //cancel button
        private void btn_cancel(object sender, EventArgs e)
        {
            maindelete.Enabled = true;
            mainupdate.Enabled = true;
            Binsert.Enabled = true;
            disableText();
            unshowOK();
            clearAll();
        }

        //enabling ok buttons
        public void showOK()
        {
            Bcancel.Visible = true;
            ok.Visible = true;
        }

        //disabling ok buttons
        public void unshowOK()
        {
            Bcancel.Visible = false;
            ok.Visible = false;
        }

        //enabling all textboxes
        public void enableText()
        {
            txt_address.Enabled = true;
            txt_cellPhone.Enabled = true;
            txt_fName.Enabled = true;
            txt_homePhone.Enabled = true;
            txt_lName.Enabled = true;
            txt_payment.Enabled = true;
            txt_ID.Enabled = true;
        }

        //disabling all textboxes
        public void disableText()
        {
            txt_address.Enabled = false;
            txt_cellPhone.Enabled = false;
            txt_fName.Enabled = false;
            txt_homePhone.Enabled = false;
            txt_lName.Enabled = false;
            txt_payment.Enabled = false;
        }

        //searching a costumer
        public Boolean costumerSearch(int idNum){

             int i = 0;
            string query1 = "select costumerID from tbl_costumers";
            DataTable dt1 = AdoHelper.ExecuteDataTable(query1);
            for (i = 0; i < dt1.Rows.Count; i++)
                if (idNum == (int)(dt1.Rows[i].ItemArray[0]))
                    break;

            if (i >= 0 && i < dt1.Rows.Count)
                return true;
            else
                return false;
            }
    }
}
